package server.main.utils.mainutils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class maven {
    public static boolean clean(){
        BufferedReader br = null;
        try {
            Process p = Runtime.getRuntime().exec("mvn clean");
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(line.contains("ERROR")){
                    System.out.println("出错啦！");
                }
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("clean成功");
        return true;
    }

    public static boolean repackage(){
        BufferedReader br = null;
        try {
            Process p = Runtime.getRuntime().exec("mvn package");
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(line.contains("ERROR")){
                    System.out.println("出错啦！");
                }
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("打包成功");

        return true;
    }

    public static boolean maven_pck(String path,String... opt){
        String str_opt = "cmd /c cd ";
        str_opt += path;
        for (String s : opt) {
            if(s!= null &&!"".equals(s)){
                str_opt += " &&" + s;
            }
        }
        System.out.println("执行指令"+str_opt);
        BufferedReader br = null;
        try {
            System.out.println("正在打包请等候--------------");
            Process p = Runtime.getRuntime().exec(str_opt);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(line.contains("ERROR")){
                    System.out.println("出错啦！");
                }
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("打包成功");
        return true;
    }

    /**
     * 调用外部java程序，获得项目的基本信息，并将项目停止，并启动相应的版本
     * @param path 存储要调用的Java文件的路由
     *
     * @return
     */

    public static boolean restart(String path){

        //先到目标文件夹中
        String jarname = "BloodTransfusionManagement-";
        String configpath = "";
        String version = Getjsonparam(configpath, "version");
        String jarpath = Getjsonparam(configpath, "path");
        jarname = jarname + version + ".jar";
        String str_opt = "cmd /c cd ";
        str_opt += jarpath;
//        JsonUtils.readFile(path + "config.json");
        //将获取到版本信息，定位文件位置
        BufferedReader br = null;
        try {
            System.out.println("正在打包请等候--------------");
            Process p = Runtime.getRuntime().exec(str_opt);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if(line.contains("ERROR")){
                    System.out.println("出错啦！");
                }
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


        //获得command

        return true;
    }
    public static String Getjsonparam(String path,String param){
        String mainfilepath = JsonUtils.readFile(path);
        JSONObject myJson = null;
        try {
            myJson = new JSONObject(mainfilepath);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return  myJson.optString(param);
    }

//    public static void main(String[] args) {
//        maven_pck();
//
//    }
}
